package com.iotechn.unimall.admin.api.treeCategory;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.TreeCategoryDO;
import com.iotechn.unimall.data.domain.TreeDO;
import com.iotechn.unimall.data.mapper.TreeCategoryMapper;
import com.iotechn.unimall.data.mapper.TreeMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class AdminTreeCategoryServiceImpl implements AdminTreeCategoryService {

    @Autowired
    private TreeCategoryMapper treeCategoryMapper;
    @Autowired
    private TreeMapper treeMapper;

    @Override
    public Page<TreeCategoryDO> list(Integer page, Integer limit, String name, Long adminId) throws ServiceException {
        Wrapper<TreeCategoryDO> wrapper = new EntityWrapper<TreeCategoryDO>();
        wrapper.setSqlSelect("id", "`name`");
        if (!StringUtils.isEmpty(name))
            wrapper.like("name", name);
        wrapper.orderBy("id", false);
        Integer count = treeCategoryMapper.selectCount(wrapper);
        List<TreeCategoryDO> TreeCategoryDOS = treeCategoryMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(TreeCategoryDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TreeCategoryDO create(TreeCategoryDO treeCategoryDO, Long adminId) throws ServiceException {
        Wrapper<TreeCategoryDO> wrapper = new EntityWrapper<TreeCategoryDO>()
                .eq("name", treeCategoryDO.getName());
        if (treeCategoryMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_PARTITION_CANNOT_REPEATED);
        if (treeCategoryMapper.insert(treeCategoryDO) > 0) {
            treeCategoryDO.setId(treeCategoryDO.getId());
            return treeCategoryDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(TreeCategoryDO treeCategoryDO, Long adminId) throws ServiceException {
        Wrapper<TreeCategoryDO> wrapper = new EntityWrapper<TreeCategoryDO>()
                .eq("name", treeCategoryDO.getName())
                .ne("id", treeCategoryDO.getId());
        if (treeCategoryMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_PARTITION_CANNOT_REPEATED);
        Long id = treeCategoryDO.getId();
        if (id == null) return "fail";
        if (treeCategoryMapper.updateById(treeCategoryDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        Wrapper<TreeDO> wrapper = new EntityWrapper<TreeDO>().eq("category_id", id);
        if (treeMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_CATEGORY_CANNOT_DELETE);
        if (treeCategoryMapper.deleteById(id) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public TreeCategoryDO detail(Long id, Long adminId) throws ServiceException {
        return treeCategoryMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        Wrapper<TreeDO> wrapper = new EntityWrapper<TreeDO>().in("category_id", ids);
        if (treeMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_CATEGORY_CANNOT_DELETE);
        if (treeCategoryMapper.delete(new EntityWrapper<TreeCategoryDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        return "ok";
    }

    @Override
    public List<TreeCategoryDO> select(Long adminId) {
        Wrapper<TreeCategoryDO> wrapper = new EntityWrapper<TreeCategoryDO>().setSqlSelect("id", "name").orderBy("id", false);
        return treeCategoryMapper.selectList(wrapper);
    }
}
